#include "pch.h"
#include "CoreFilter.h"




const QString CoreFilter::m_name        = "Core Filter";
const QString CoreFilter::m_description = "This is a Core Filter: It filters replies and URLs based on the (suspected) content of the page/URL: It only accepts text-like documents";
const QString CoreFilter::m_version     = "0.1a";




inline int qHash( const QUrl &url )
{
	return qHash( url.toString() );
}
///////////////////////////////////////////////////////////////////////////////////////////////////




CoreFilter::CoreFilter()
	: m_enabled( true )
{
	// Basically, we accept all types, that are text files and can be parsed by our
	// Regexp analyzer.
	// The CSV and TSV types are very interesting, as they are used for database
	// storage (sometimes) and might contain alot of valuable information
	m_contentTypes
		<< "text/plain"
		<< "text/html"
		<< "text/xml"
		<< "text/css"
		<< "text/javascript"
		<< "text/comma-separated-values"
		<< "text/tab-separated-values";
}
///////////////////////////////////////////////////////////////////////////////////////////////////




QString CoreFilter::name() const
{
	return m_name;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

QString CoreFilter::description() const
{
	return m_description;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

QString CoreFilter::version() const
{
	return m_version;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




bool CoreFilter::filterReply( const QNetworkReply *reply ) const
{
	if( reply->error() != QNetworkReply::NoError )
		return false;

	if( !reply->hasRawHeader( "Content-Type" ) )
		return false;

	QString contentType = reply->rawHeader( "Content-Type" );

	//
	// Policy: If the Content-Type matches one of our allowed types, then it is accepted
	//
	foreach( const QString &type, m_contentTypes )
	{
		if( contentType.contains( type ) )
			return true;
	}

	return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




bool CoreFilter::filterUrl( const QUrl &url ) const
{
	QSet<QUrl>::const_iterator i = m_acceptedUrls.find( url );
	if( i != m_acceptedUrls.end() )
		return false;

	QString path = url.path();
	if( path.endsWith( ".jpg" ) ||
		path.endsWith( ".bmp" ) ||
		path.endsWith( ".png" ) ||
		path.endsWith( ".gif" ) )
		return false;

	return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void CoreFilter::urlPassed( const QUrl &url )
{
	m_acceptedUrls << url;
}
///////////////////////////////////////////////////////////////////////////////////////////////////








void CoreFilter::enable()
{
	m_enabled = true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void CoreFilter::disable()
{
	m_enabled = false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




bool CoreFilter::enabled() const
{
	return m_enabled;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

bool CoreFilter::disabled() const
{
	return !m_enabled;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




PluginRoot *createPlugin()
{
	return new CoreFilter();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void releasePlugin( PluginRoot *object )
{
	delete object;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
